package com.ztz.mynewdemo.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.ztz.mynewdemo.R;
import com.ztz.mynewdemo.adapter.CollectAdapter;
import com.ztz.mynewdemo.bean.CollectResponse;
import com.ztz.mynewdemo.bean.MyDatabaseHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CollectActivity extends BaseActivity {
    ListView lv;
    String url;
    MyDatabaseHelper dbHelper;
    CollectAdapter collectAdapter;
    SearchView sv;
    private SimpleAdapter adapter;
    boolean collection=true;
    List<CollectResponse> collectResponseList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_collect);
        lv=(ListView)findViewById(R.id.lv_collect);
        sv=(SearchView)findViewById(R.id.sv);
        setToolbar("收藏");
        dbHelper=new MyDatabaseHelper(this,"CollectionStore.db",null,4);
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        Cursor cursor=db.query("Collection",null,null,null,null,null,null);
        while (cursor.moveToNext()) {
            int type=cursor.getInt(cursor.getColumnIndex("type"));
            String title = cursor.getString(cursor.getColumnIndex("title"));
            String date = cursor.getString(cursor.getColumnIndex("date"));
            String category = cursor.getString(cursor.getColumnIndex("category"));
            String author = cursor.getString(cursor.getColumnIndex("author_name"));
            String image = cursor.getString(cursor.getColumnIndex("image"));
            url = cursor.getString(cursor.getColumnIndex("url"));
            collectResponseList.add(new CollectResponse(type,title, date, category, author, image,url));
        }
        //SimpleAdapter是扩展性最好的适配器，可以定义各种你想要的布局，而且使用很方便
        //SimpleAdapter就是用来装静态数据的，要显示动态数据的话要自己实现一个适配器
//        adapter = new SimpleAdapter(this,getData(), R.layout.item_collecton,new String[]{"title","date","category","author","image"},
//                new int[]{R.id.top_title,R.id.topLine_data,R.id.tv_category,R.id.tv_author,R.id.iv_top});
        final boolean[] isStart = {true};
        collectAdapter=new CollectAdapter(CollectActivity.this,collectResponseList);
        lv.setAdapter(collectAdapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                if(isStart[0]) {
                    Intent intent = new Intent(CollectActivity.this, CollectedActivity.class);
                    intent.putExtra(CollectedActivity.URL, collectResponseList.get(position).getUrl());
                    startActivity(intent);
                }

            }
        });
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                isStart[0] =false;
                AlertDialog.Builder alert=new AlertDialog.Builder(CollectActivity.this)
                        .setTitle("删除收藏")
                        .setMessage("标题:"+collectResponseList.get(position).getTitle())
                        .setCancelable(false)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                SQLiteDatabase db=dbHelper.getWritableDatabase();
                                db.delete("Collection","title=?",new String[]{collectResponseList.get(position).getTitle()});
                                collectResponseList.remove(position);
                                collectAdapter.notifyDataSetChanged();
                                isStart[0]=true;
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                isStart[0]=true;
                            }
                        });
                alert.show();
                return false;
            }
        });
        lv.setTextFilterEnabled(true);
        sv.setQueryHint("查找");
        sv.setIconifiedByDefault(false);
        sv.setSubmitButtonEnabled(true);
        sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if(TextUtils.isEmpty(newText))
                {
                    lv.clearTextFilter();
                }
                else
                {
                    lv.setFilterText(newText);
                }
                return false;
            }
        });
    }
    private List<Map<String,Object>> getData(){
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        Cursor cursor=db.query("Collection",null,null,null,null,null,null);
        List<Map<String,Object>> list=new ArrayList<>();

        while (cursor.moveToNext()){
            int type=cursor.getInt(cursor.getColumnIndex("type"));
            String title=cursor.getString(cursor.getColumnIndex("title"));
            String date=cursor.getString(cursor.getColumnIndex("date"));
            String category=cursor.getString(cursor.getColumnIndex("category"));
            String author=cursor.getString(cursor.getColumnIndex("author_name"));
            String image=cursor.getString(cursor.getColumnIndex("image"));
            url = cursor.getString(cursor.getColumnIndex("url"));
            collectResponseList.add(new CollectResponse(type,title,date,category,author,image,url));
//            Log.d("TAG",url);
            Map<String,Object> map=new HashMap<>();
//            map.put("url",url);
            map.put("title",title);
            map.put("date",date);
            map.put("category",category);
            map.put("author",author);
            map.put("image",image);
            list.add(map);
//           lv.invalidateViews();
        }
        return list;

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
